package Mobil;

import Mobil.Mobil;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import Kasir.*;

/**
 *
 * @author  Astri
 */
public class MobilForm extends javax.swing.JFrame {

    /** Creates new form MobilForm */
    public MobilForm() {
        try {
            initComponents();
            simpanButton.setEnabled(false);


            List<Mobil> mobil = MobilKontrol.getKoneksiKontrol().getAllMobil();
            MobilTabelModel model = new MobilTabelModel(mobil);
            mobilTable.setModel(model);

        } catch (Exception e) {
            Logger.getLogger(MobilForm.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        passwordLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        kodeMobilLabel = new javax.swing.JLabel();
        tipeMobilLabel = new javax.swing.JLabel();
        kodeMobilField = new javax.swing.JTextField();
        tipeMobilField = new javax.swing.JTextField();
        simpanButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();
        keluarButton = new javax.swing.JButton();
        nopolLabel = new javax.swing.JLabel();
        nopolField = new javax.swing.JTextField();
        warnaLabel = new javax.swing.JLabel();
        warnaField = new javax.swing.JTextField();
        statusLabel = new javax.swing.JLabel();
        hargaSewaLabel = new javax.swing.JLabel();
        hargaSewaField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        statusMobilComboBox = new javax.swing.JComboBox();
        ubahButton = new javax.swing.JButton();
        hapusButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        mobilTable = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        kembaliButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        passwordLabel1.setText("Password");

        jLabel4.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel4.setForeground(new java.awt.Color(255, 0, 0));
        jLabel4.setText("*ex : AVANSA G");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(102, 102, 102));

        jPanel1.setBackground(new java.awt.Color(0, 0, 0));

        kodeMobilLabel.setForeground(new java.awt.Color(255, 255, 0));
        kodeMobilLabel.setText("Kode Mobil");

        tipeMobilLabel.setForeground(new java.awt.Color(255, 255, 0));
        tipeMobilLabel.setText("Tipe Mobil");

        kodeMobilField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kodeMobilFieldActionPerformed(evt);
            }
        });
        kodeMobilField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                kodeMobilFieldKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                kodeMobilFieldKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                kodeMobilFieldKeyTyped(evt);
            }
        });

        tipeMobilField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipeMobilFieldActionPerformed(evt);
            }
        });
        tipeMobilField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tipeMobilFieldKeyTyped(evt);
            }
        });

        simpanButton.setText("Simpan");
        simpanButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                simpanButtonActionPerformed(evt);
            }
        });

        batalButton.setText("Batal");
        batalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                batalButtonActionPerformed(evt);
            }
        });

        keluarButton.setText("Keluar");
        keluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluarButtonActionPerformed(evt);
            }
        });

        nopolLabel.setForeground(new java.awt.Color(255, 255, 0));
        nopolLabel.setText("Nomor Polisi");

        nopolField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nopolFieldActionPerformed(evt);
            }
        });

        warnaLabel.setForeground(new java.awt.Color(255, 255, 0));
        warnaLabel.setText("Warna");

        warnaField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                warnaFieldActionPerformed(evt);
            }
        });

        statusLabel.setForeground(new java.awt.Color(255, 255, 0));
        statusLabel.setText("Status Mobil");

        hargaSewaLabel.setForeground(new java.awt.Color(255, 255, 0));
        hargaSewaLabel.setText("Harga Sewa");

        hargaSewaField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hargaSewaFieldActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel1.setForeground(new java.awt.Color(255, 0, 0));
        jLabel1.setText("*RM_NomorMobil, ex : RM_001");

        jLabel2.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("*ex : AVANSA G");

        jLabel6.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("*ex : AD2222BB");

        statusMobilComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Disewakan", "Tidak Disewakan" }));

        ubahButton.setText("ubah");
        ubahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahButtonActionPerformed(evt);
            }
        });

        hapusButton.setText("Hapus");
        hapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hapusButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(tipeMobilLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(warnaLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(kodeMobilLabel)
                                        .addComponent(statusLabel))
                                    .addGap(37, 37, 37))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(hargaSewaLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(nopolLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(hargaSewaField, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(nopolField, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(tipeMobilField, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(kodeMobilField, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
                            .addComponent(warnaField, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(simpanButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ubahButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(hapusButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(batalButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(keluarButton)
                        .addGap(52, 52, 52))
                    .addComponent(statusMobilComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(kodeMobilLabel)
                    .addComponent(kodeMobilField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(tipeMobilLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nopolLabel)
                        .addGap(18, 18, 18)
                        .addComponent(warnaLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tipeMobilField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nopolField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(warnaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(statusLabel)
                    .addComponent(statusMobilComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hargaSewaField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hargaSewaLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(simpanButton)
                    .addComponent(ubahButton)
                    .addComponent(batalButton)
                    .addComponent(keluarButton)
                    .addComponent(hapusButton))
                .addContainerGap())
        );

        mobilTable.setAutoCreateRowSorter(true);
        mobilTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Kode Mobil", "Tipe Mobil", "Nomor Polisi", "Warna", "Status Mobil", "Harga Sewa"
            }
        ));
        mobilTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mobilTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(mobilTable);

        jLabel5.setText("Data Mobil");

        jPanel2.setBackground(new java.awt.Color(102, 0, 0));

        jLabel3.setBackground(new java.awt.Color(204, 204, 255));
        jLabel3.setFont(new java.awt.Font("Tw Cen MT Condensed Extra Bold", 0, 24));
        jLabel3.setForeground(new java.awt.Color(255, 255, 0));
        jLabel3.setText("UPDATE MOBIL");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(298, 298, 298)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(280, 280, 280))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
        );

        kembaliButton.setText("Kembali");
        kembaliButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kembaliButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addContainerGap(654, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 694, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(600, Short.MAX_VALUE)
                .addComponent(kembaliButton)
                .addGap(45, 45, 45))
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(kembaliButton)
                .addGap(95, 95, 95))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-730)/2, (screenSize.height-535)/2, 730, 535);
    }// </editor-fold>//GEN-END:initComponents

private void tipeMobilFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipeMobilFieldActionPerformed
// TODO add your handling code here:
    simpanButton.setEnabled(true);
    simpanButton.requestFocus();


}//GEN-LAST:event_tipeMobilFieldActionPerformed

private void keluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluarButtonActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_keluarButtonActionPerformed

private void batalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_batalButtonActionPerformed
// TODO add your handling code here:
    kodeMobilField.setText("");
    tipeMobilField.setText("");
    nopolField.setText("");
    warnaField.setText("");
    statusMobilComboBox.setSelectedItem("");
    hargaSewaField.setText("");
    kodeMobilField.setEditable(true);
    simpanButton.setText("Simpan");
}//GEN-LAST:event_batalButtonActionPerformed

private void simpanButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_simpanButtonActionPerformed

    try {

        String kode = kodeMobilField.getText();
        String tipe = tipeMobilField.getText();
        String nopol = nopolField.getText();
        String wrn = warnaField.getText();
        String stt = statusMobilComboBox.getSelectedItem().toString();
        Double hs = Double.parseDouble(hargaSewaField.getText());

        Mobil mobilBaru = new Mobil();
        mobilBaru.setKode_Mobil(kode);
        mobilBaru.setTipe_Mobil(tipe);
        mobilBaru.setNomor_Polisi(nopol);
        mobilBaru.setWarna(wrn);
        mobilBaru.setStatus_Mobil(stt);
        mobilBaru.setHarga_Sewa(hs);

        MobilKontrol mobilKontrol = MobilKontrol.getKoneksiKontrol();
        if (simpanButton.getText() == "Simpan") {

            mobilKontrol.simpanMobil(mobilBaru);
            List<Mobil> mobil = MobilKontrol.getKoneksiKontrol().getAllMobil();
            MobilTabelModel model = new MobilTabelModel(mobil);
            mobilTable.setModel(model);
            JOptionPane.showMessageDialog(this, "Mobil dengan kode " + kode + " berhasil disimpan");

        }

    } catch (SQLException ex) {
        Logger.getLogger(MobilForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "Data gagal disimpan");

    }
    batalButton.doClick();


}//GEN-LAST:event_simpanButtonActionPerformed

private void mobilTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mobilTableMouseClicked
    int row = mobilTable.getSelectedRow();
    String kodegroup = mobilTable.getValueAt(row, 0).toString();
    String tipegroup = mobilTable.getValueAt(row, 1).toString();
    String nopolgroup = mobilTable.getValueAt(row, 2).toString();
    String wrngroup = mobilTable.getValueAt(row, 3).toString();
    String sttgroup = mobilTable.getValueAt(row, 4).toString();
    String hsgroup = mobilTable.getValueAt(row, 5).toString();

    kodeMobilField.setText(kodegroup);
    tipeMobilField.setText(tipegroup);    
    nopolField.setText(nopolgroup);
    warnaField.setText(wrngroup);
    statusMobilComboBox.setSelectedItem(sttgroup);
    hargaSewaField.setText(hsgroup);

    kodeMobilField.setEditable(false);
    tipeMobilField.requestFocus();

    simpanButton.setEnabled(true);
    //simpanButton.setText("Update");
    //hapusButton.setEnabled(true);


}//GEN-LAST:event_mobilTableMouseClicked

private void kodeMobilFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kodeMobilFieldActionPerformed
}//GEN-LAST:event_kodeMobilFieldActionPerformed

private void kodeMobilFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeMobilFieldKeyReleased
    
    Mobil mobil = new Mobil();
    String kode = kodeMobilField.getText();
    mobil.setKode_Mobil(kode);

    try {
        mobil = MobilKontrol.getKoneksiKontrol().cariMobil(mobil);
        String namaMobil = mobil.getTipe_Mobil();
        tipeMobilField.setText(namaMobil);
    } catch (Exception e) {
        Logger.getLogger(MobilForm.class.getName()).log(Level.SEVERE, null, e);

    }
}//GEN-LAST:event_kodeMobilFieldKeyReleased

private void kodeMobilFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeMobilFieldKeyPressed
    //harus pakai enter baru nama kategori muncul
}//GEN-LAST:event_kodeMobilFieldKeyPressed

private void kodeMobilFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_kodeMobilFieldKeyTyped
    //supaya nama kategori muncul, tekan enter
    if (evt.getSource().equals(kodeMobilField)) {
        if (kodeMobilField.getText().length() > 10) {
            evt.setKeyChar('\n');
            JOptionPane.showMessageDialog(null, "id maksimal 9 karakter");

        }
    }

}//GEN-LAST:event_kodeMobilFieldKeyTyped

private void tipeMobilFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tipeMobilFieldKeyTyped
    if (!kodeMobilField.getText().isEmpty()) {
        simpanButton.setEnabled(true);
    } else {
        JOptionPane.showMessageDialog(null, "id harus dimasukkan");
    }

}//GEN-LAST:event_tipeMobilFieldKeyTyped

private void kembaliButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kembaliButtonActionPerformed
    // TODO add your handling code here:
    new FrameUtamaKasir().setVisible(true);
            this.setVisible(false);
}//GEN-LAST:event_kembaliButtonActionPerformed

private void nopolFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nopolFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_nopolFieldActionPerformed

private void warnaFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_warnaFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_warnaFieldActionPerformed

private void hargaSewaFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hargaSewaFieldActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_hargaSewaFieldActionPerformed

private void ubahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubahButtonActionPerformed
    // TODO add your handling code here:
   
    try {

        String kode = kodeMobilField.getText();
        String tipe = tipeMobilField.getText();
        String nopol = nopolField.getText();
        String wrn = warnaField.getText();
        String stt = statusMobilComboBox.getSelectedItem().toString();
        Double hs = Double.parseDouble(hargaSewaField.getText());

        Mobil mobilBaru = new Mobil();
        mobilBaru.setKode_Mobil(kode);
        mobilBaru.setTipe_Mobil(tipe);
        mobilBaru.setNomor_Polisi(nopol);
        mobilBaru.setWarna(wrn);
        mobilBaru.setStatus_Mobil(stt);
        mobilBaru.setHarga_Sewa(hs);

        MobilKontrol mobilKontrol = MobilKontrol.getKoneksiKontrol();
        mobilKontrol.ubahMobil(mobilBaru);

        List<Mobil> mo = MobilKontrol.getKoneksiKontrol().getAllMobil();
        MobilTabelModel model = new MobilTabelModel(mo);
        mobilTable.setModel(model);
        JOptionPane.showMessageDialog(this, " " + kode + " berhasil diubah");
    } catch (SQLException ex) {
        Logger.getLogger(MobilForm.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(this, "Data Mobil gagal diubah");
    }
    dataKosong();
}//GEN-LAST:event_ubahButtonActionPerformed

private void hapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hapusButtonActionPerformed
    // TODO add your handling code here:
    try {

            String kode = kodeMobilField.getText();
            String tipe = tipeMobilField.getText();
            Mobil mobilBaru = new Mobil();
            mobilBaru.setKode_Mobil(kode);
            mobilBaru.setTipe_Mobil(tipe);


            MobilKontrol mobilKontrol = MobilKontrol.getKoneksiKontrol();
            mobilKontrol.hapusMobil(mobilBaru);

            List<Mobil> mo = MobilKontrol.getKoneksiKontrol().getAllMobil();
            MobilTabelModel model = new MobilTabelModel(mo);
            mobilTable.setModel(model);

            JOptionPane.showMessageDialog(this, " " + kode + " berhasil dihapus");
        } catch (SQLException ex) {
            Logger.getLogger(MobilForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this, "Data mobil gagal dihapus");
        }
        dataKosong();
    
}//GEN-LAST:event_hapusButtonActionPerformed

    public void dataKosong() {
        kodeMobilField.setEditable(true);
        tipeMobilField.setText("");
        nopolField.setText("");
        warnaField.setText("");
        statusMobilComboBox.setSelectedItem(null);
        hargaSewaField.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MobilForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalButton;
    private javax.swing.JButton hapusButton;
    private javax.swing.JTextField hargaSewaField;
    private javax.swing.JLabel hargaSewaLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton keluarButton;
    private javax.swing.JButton kembaliButton;
    private javax.swing.JTextField kodeMobilField;
    private javax.swing.JLabel kodeMobilLabel;
    private javax.swing.JTable mobilTable;
    private javax.swing.JTextField nopolField;
    private javax.swing.JLabel nopolLabel;
    private javax.swing.JLabel passwordLabel1;
    private javax.swing.JButton simpanButton;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JComboBox statusMobilComboBox;
    private javax.swing.JTextField tipeMobilField;
    private javax.swing.JLabel tipeMobilLabel;
    private javax.swing.JButton ubahButton;
    private javax.swing.JTextField warnaField;
    private javax.swing.JLabel warnaLabel;
    // End of variables declaration//GEN-END:variables
}
